/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/*
 *@Note
 *task1 and task2 alternate printing
 */

#include "main.h"

/* Global define */
#define TASK1_TASK_PRIO 5
#define TASK1_STK_SIZE 256
#define TASK2_TASK_PRIO 5
#define TASK2_STK_SIZE 256

/* Global Variable */
TaskHandle_t Task1Task_Handler;
TaskHandle_t Task2Task_Handler;
extern void net_task(void *arg);
extern void net_init();
TaskHandle_t NetTask_Handler;
void delay()
{
    for (__IO int i = 0; i < 0xfffff; i++)
    {
        asm("nop");
    }
}
/*********************************************************************
 * @fn      GPIO_Toggle_INIT
 *
 * @brief   Initializes GPIOA.0/1
 *
 * @return  none
 */
void GPIO_Toggle_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/*********************************************************************
 * @fn      task1_task
 *
 * @brief   task1 program.
 *
 * @param  *pvParameters - Parameters point of task1
 *
 * @return  none
 */
void task1_task(void *pvParameters)
{
    while (1)
    {
        // printf("task1 entry\r\n");
        GPIO_SetBits(GPIOC, GPIO_Pin_0);
        vTaskDelay(500);
        GPIO_ResetBits(GPIOC, GPIO_Pin_0);
        vTaskDelay(500);
    }
}

/*********************************************************************
 * @fn      task2_task
 *
 * @brief   task2 program.
 *
 * @param  *pvParameters - Parameters point of task2
 *
 * @return  none
 */
void task2_task(void *pvParameters)
{
    while (1)
    {
        printf("task2 entry\r\n");
        GPIO_ResetBits(GPIOC, GPIO_Pin_1);
        vTaskDelay(500);
        GPIO_SetBits(GPIOC, GPIO_Pin_1);
        vTaskDelay(500);
    }
}

void task_tick(void *arg)
{
    static uint32_t tick = 0;
    tick = 0;
    while (1)
    {
        // printf("tick:%ld\r\n", tick);
        tick++;
        vTaskDelay(1000);
    }
}
/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    uwTick = 0;

    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
    printf("FreeRTOS Kernel Version:%s\r\n", tskKERNEL_VERSION_NUMBER);

    GPIO_Toggle_INIT();

    printf("led flash\r\n");
    for (int i = 0; i < 5; i++)
    {
        GPIO_SetBits(GPIOC, GPIO_Pin_0);
        delay();
        GPIO_ResetBits(GPIOC, GPIO_Pin_0);
        delay();
    }

    net_init();

    xTaskCreate(task1_task, "task1", 256, NULL, 1, NULL);
    xTaskCreate(task_tick, "tick", 256, NULL, 2, NULL);
    xTaskCreate(net_task, "net_task", 10 * 256, NULL, 4, &NetTask_Handler);

    vTaskStartScheduler();

    while (1)
    {
        printf("shouldn't run at here!!\n");
    }
}

void vApplicationStackOverflowHook(TaskHandle_t xTask,
                                   char *pcTaskName)
{
}

static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
    *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
    *ppxIdleTaskStackBuffer = &xIdleStack[0];
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
    /* place for user code */
}